import { parse } from 'url';
import webpath from '@/utils/webpath';

// mock tableListDataSource
let tableListDataSource = [];
for (let i = 0; i < 46; i += 1) {
    tableListDataSource.push({
        recordid: `李海波 recordid ${i}`,
        id: `李海波 id ${i}`,
        text: `李海波 text ${i}`,
        parentid: `李海波 parentid ${i}`,
        indexs: `李海波 indexs ${i}`,
        icon: `李海波 icon ${i}`,
        sequences: `李海波 sequences ${i}`,
        href: `李海波 href ${i}`,
        hreftarget: `李海波 hreftarget ${i}`,
        enable: `李海波 enable ${i}`,
        valid: `李海波 valid ${i}`,
        leaf: `李海波 leaf ${i}`,
        systemid: `李海波 systemid ${i}`,
        isframe: `李海波 isframe ${i}`,
        levelcode: `李海波 levelcode ${i}`,
        leaftype: `李海波 leaftype ${i}`,
        cls: `李海波 cls ${i}`,
        component: `李海波 component ${i}`,
        i18n_name: `李海波 i18n_name ${i}`,
        redirect: `李海波 redirect ${i}`,
        hideInMenu: `李海波 hideInMenu ${i}`,
        hideChildrenInMenu: `李海波 hideChildrenInMenu ${i}`,
        status: Math.floor(Math.random() * 10) % 4,
    });
}

function getProMenu(req, res, u) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const params = parse(url, true).query;

    let dataSource = tableListDataSource;

    if (params.sorter!=undefined) {
        const s = params.sorter.split('_');
        dataSource = dataSource.sort((prev, next) => {
            if (s[1] === 'descend') {
            return next[s[0]] - prev[s[0]];
        }
        return prev[s[0]] - next[s[0]];
    });
    }

    if (params.status!=undefined) {
    const status = params.status.split(',');
    let filterDataSource = [];
    status.forEach(s => {
    filterDataSource = filterDataSource.concat(
    dataSource.filter(data =>
    {

        if(data.status!=undefined){
        return parseInt(data.status, 10) === parseInt(s[0], 10);
        }
    })
    );
    });
    dataSource = filterDataSource;
    }

    if (params.name!=undefined) {
    dataSource = dataSource.filter(data => {

        if(data.name!=undefined){
        return data.name.indexOf(params.name) > -1;
        }

    });
    }

    let pageSize = 10;
    if (params.pageSize!=undefined) {
        pageSize = params.pageSize * 1;
    }

    const result = {
        list: dataSource,
        pagination: {
            total: dataSource.length,
            pageSize,
            current: parseInt(params.currentPage, 10) || 1,
        },
    };

    return res.json(result);
}



function deleteByPrimaryKeyForProMenu(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;

    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



    return getProMenu(req, res, u);
}


function selectByPrimaryKeyForProMenu(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProMenu(req, res, u);
}



function insertForProMenu(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;



    const i = Math.ceil(Math.random() * 10000);
    tableListDataSource.unshift({
        recordid: `李海波 recordid ${i}`,
        id: `李海波 id ${i}`,
        text: `李海波 text ${i}`,
        parentid: `李海波 parentid ${i}`,
        indexs: `李海波 indexs ${i}`,
        icon: `李海波 icon ${i}`,
        sequences: `李海波 sequences ${i}`,
        href: `李海波 href ${i}`,
        hreftarget: `李海波 hreftarget ${i}`,
        enable: `李海波 enable ${i}`,
        valid: `李海波 valid ${i}`,
        leaf: `李海波 leaf ${i}`,
        systemid: `李海波 systemid ${i}`,
        isframe: `李海波 isframe ${i}`,
        levelcode: `李海波 levelcode ${i}`,
        leaftype: `李海波 leaftype ${i}`,
        cls: `李海波 cls ${i}`,
        component: `李海波 component ${i}`,
        i18n_name: `李海波 i18n_name ${i}`,
        redirect: `李海波 redirect ${i}`,
        hideInMenu: `李海波 hideInMenu ${i}`,
        hideChildrenInMenu: `李海波 hideChildrenInMenu ${i}`,
        status: Math.floor(Math.random() * 10) % 4,
    });


    return getProMenu(req, res, u);
}




function updateForProMenu(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;

    if(id!=undefined)
    tableListDataSource = tableListDataSource.map(item => {
        if (item.id === id) {
        Object.assign(item, { desc, name });
        return item;
    }
    return item;
});


    return getProMenu(req, res, u);
}


function deleteByMapForProMenu(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProMenu(req, res, u);
}



function updateByMapForProMenu(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProMenu(req, res, u);
}

function getOneForProMenu(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(undefined!=id)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



    return getProMenu(req, res, u);
}

function getAllForProMenu(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



    return getProMenu(req, res, u);
}

function queryPageForProMenu(req, res, u, b) {


    return getProMenu(req, res, u);
}

function countForProMenu(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProMenu(req, res, u);
}


function insertBatchForProMenu(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProMenu(req, res, u);
}



function deleteBatchForProMenu(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProMenu(req, res, u);
}


function updateBatchForProMenu(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProMenu(req, res, u);
}




export default {
    'GET /${webpath}/web/api/service/promenu/delete': deleteByPrimaryKeyForProMenu,
    'GET /${webpath}/web/api/service/promenu/get': selectByPrimaryKeyForProMenu,
    'POST /${webpath}/web/api/service/promenu/insert': insertForProMenu,
    'POST /${webpath}/web/api/service/promenu/update': updateForProMenu,
    'POST /${webpath}/web/api/service/promenu/deleteByMap': deleteByMapForProMenu,
    'POST /${webpath}/web/api/service/promenu/updateByMap': updateByMapForProMenu,
    'POST /${webpath}/web/api/service/promenu/getOne': getOneForProMenu,
    'POST /${webpath}/web/api/service/promenu/getAll': getAllForProMenu,
    'POST /${webpath}/web/api/service/promenu/queryPage': queryPageForProMenu,
    'POST /${webpath}/web/api/service/promenu/count': countForProMenu,
    'POST /${webpath}/web/api/service/promenu/insertBatch': insertBatchForProMenu,
    'POST /${webpath}/web/api/service/promenu/deleteBatch': deleteBatchForProMenu,
    'POST /${webpath}/web/api/service/promenu/updateBatch': updateBatchForProMenu,
};
